//
//  PhotosAndLB.m
//  Photos
//
//  Created by 范琦 on 15/11/2.
//  Copyright (c) 2015年 范琦. All rights reserved.
//

#import "PhotosAndLB.h"
#import "UIImageView+WebCache.h"


@interface PhotosAndLB()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scroll;

@property (nonatomic, strong) NSMutableArray *arrr;



@end

@implementation PhotosAndLB

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:self.scroll];
        
        self.scroll.delegate = self;
        self.scroll.pagingEnabled = YES;
//        self.scroll.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    
    }
    return self;


}

- (void)setArr:(NSMutableArray *)arr photoStyles:(photosNetOrBundle)photoStyle placeholderImage:(NSString *)placeholderImage pageControl:(CGRect)rect
{
    _arr = [NSMutableArray arrayWithArray:arr];
    self.scroll.contentSize = CGSizeMake(self.frame.size.width * (arr.count + 1), 0);
    
//    self.arrr = [NSMutableArray arrayWithObjects:arr[0], arr[1], arr[2], arr[3], arr[4], arr[0], nil];
    
    self.arrr = [NSMutableArray arrayWithArray:arr];
    [self.arrr addObject:arr[0]];
    
    for (int i = 0; i < self.arrr.count; i++) {
        
        UIImageView *imageVV = [self viewWithTag:700 + i];
        if (imageVV == nil) {
            
            imageVV = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.scroll.bounds.size.width, 0, self.scroll.bounds.size.width, self.scroll.bounds.size.height)];
        }
        imageVV.userInteractionEnabled = YES;
            imageVV.tag = 700 + i;
            if (photoStyle == PhotosBundle) {
                
                imageVV.image = [UIImage imageNamed:self.arrr[i]];
                if (imageVV.image == nil) {
                    imageVV.image = [UIImage imageNamed:@"placeholder"];
                }
            }
            else{
                [imageVV sd_setImageWithURL:[NSURL URLWithString:self.arrr[i]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
            }
        
        UIImageView *imageM = [[UIImageView alloc] initWithFrame:CGRectMake(5, imageVV.frame.size.height * 2/ 3 - 5, imageVV.bounds.size.width - 20, imageVV.bounds.size.height / 3)];
        imageM.image = [UIImage imageNamed:@"模糊"];
//        imageM.alpha = 0.7;
        
            // 承载文字的label
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(7, imageVV.frame.size.height * 2/ 3, imageVV.bounds.size.width - 20, imageVV.bounds.size.height / 3)];
            label.textColor = [UIColor whiteColor];
            label.tag = i + 500;
            label.font = [UIFont systemFontOfSize:16];
            [imageVV addSubview:label];
        

        [imageVV addSubview:imageM];
        [self.scroll addSubview:imageVV];
        
    
        
        
    }
    if (_pageControl == nil) {
        
        self.pageControl = [[UIPageControl alloc]initWithFrame:rect];
        _pageControl.numberOfPages = arr.count;
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.pageIndicatorTintColor = [UIColor shenhuiseColor];
        [self addSubview:_pageControl];
    }else{
        _pageControl.numberOfPages = arr.count;

    
    }
}

- (void)setLabelArr:(NSMutableArray *)arr
{
    for (int i = 0; i < arr.count; i++) {
        
        UILabel *label = [self viewWithTag:i + 500];
        label.text = arr[i];
    }
    
    UILabel *label = [self viewWithTag:arr.count + 500];
    label.text = arr[0];


}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > (self.arrr.count -1) * scrollView.bounds.size.width) {
        
        
        scrollView.contentOffset = CGPointMake(0, 0);
        
    }
    if (scrollView.contentOffset.x < 0) {
        scrollView.contentOffset = CGPointMake(self.arr.count * scrollView.bounds.size.width, 0);
    }


}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = scrollView.contentOffset.x / self.scroll.bounds.size.width;
    if (scrollView.contentOffset.x / self.scroll.bounds.size.width == self.arr.count) {
        self.pageControl.currentPage = 0;
    }
}

- (void)setImageGestureRecognizers:(NSArray *)imageGestureRecognizers
{
    _imageGestureRecognizers = [NSArray arrayWithArray:imageGestureRecognizers];
    if (_arrr.count != 0) {
        for (int i = 0; i < _arrr.count; i++) {
//            
            UIImageView *imageV = (UIImageView *)[self viewWithTag:i + 700];
//            if (i == _arr.count) {
//                [imageV addGestureRecognizer:imageGestureRecognizers[0]];
//
//            }else{
//            
//                [imageV addGestureRecognizer:imageGestureRecognizers[i]];
//            }
            [imageV addGestureRecognizer:imageGestureRecognizers[i]];
//            NSLog(@"%@", imageV);
            
        }
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
